"""
对gestioipweb表进行处理。
site: https://www.gestioip.net/
"""
from requests import Session
from lxml import etree
import json

login_data = ('gipadmin', '123')
login_url = 'http://172.23.8.202/gestioip'
show_url = 'http://172.23.8.202/gestioip/ip_show.cgi'
# search_url = 'http://172.23.8.202/gestioip/ip_searchip.cgi'
# change_url_1 = 'http://172.23.8.202/gestioip/res/ip_modip_form.cgi'
change_url_2 = 'http://172.23.8.202/gestioip/res/ip_modip.cgi'
# delete_url = 'http://172.23.8.202/gestioip/res/ip_deleteip.cgi'
# param = 'bignet=0&ip_version=v4&client_id=1&red_num=2&loc=test'
show_data = {'bignet': '0', 'ip_version': 'v4', 'client_id': '1', 'red_num': '1', 'loc': 'test'}

delete_data = {
'ip_int': '167772162',
'red_num': '3',
'entries_per_page_hosts': '254',
'start_entry_hosts': '0',
'host_order_by': 'IP_auf',
'knownhosts': 'all',
'anz_values_hosts': '256',
'client_id': '1',
'ip_version': 'v4',
'B2': ''
}

change_data_1 = {
"ip": "167772161",
"hostname": "222",
"host_descr": "333",
"loc": "test",
"cat": "DB",
"int_admin": "n",
"host_exist": "yes",
"red_num": '3',
'entries_per_page_hosts': '254',
'start_entry_hosts': '0',
'host_order_by': 'IP_auf',
'knownhosts': 'all',
'anz_values_hosts': '256',
'host_id': '4',
'client_id': '1',
'ip_version': 'v4',
'B2': '',
}


session = Session()
session.auth = login_data
session.post(login_url)
# ip = '10.0.0.'
# for index in range(1, 10):
#     search_data = {
#         'search_index': 'true',
#         'hostname': ip + str(index),
#         'client_id': '1'
#     }
#     r = session.post(search_url, data=search_data)
#     html = etree.HTML(r.text)
#     data = html.xpath('//table[@id="networkTable"]/tr[@valign="middle"]/td')
#     for line in data:
#         txt = line.text
#         if txt is not None:
#             if not txt.isspace():
#                 print(txt, end=', ')
#     print()

# r = session.post(show_url, data=show_data)
# print(r.text)

# r1 = session.post(change_url_1, data=change_data_1)
# print(r1.status_code)
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Authorization': 'Basic Z2lwYWRtaW46MTIz',
'Cache-Control': 'max-age=0',
'Content-Length': '453',
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': 'GestioIPLang=en; EntriesRedPorPage=500; EntriesHostPorSite=254; scrollx=0; scrolly=0',
'Host': '172.23.8.202',
'Origin': 'http://172.23.8.202',
'Proxy-Connection': 'keep-alive',
'Referer': 'http://172.23.8.202/gestioip/res/ip_modip_form.cgi',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'
}
for i in range(1, 10):
     change_data_2 = {'ip': '192.168.0.' + str(i),
                      'hostname': 'iflytek',
                      'host_descr': 'iflytek',
                      "loc": "test",
                      "cat": "",
                      "red": "10.0.0.0",
                      "BM": "24",
                      "comentario": "iflytek",
                      "update_type": "",
                      "entries_per_page_hosts": "254",
                      "start_entry_hosts": "0",
                      "knownhosts": "all",
                      "anz_values_hosts": "256",
                      "host_id": "7",
                      "host_order_by": "IP_auf",
                      "client_id": "1",
                      "ip_version": "v4",
                      "from_line": "",
                      "line_phone_number": "",
                      "line_comment": "",
                      "line_loc_id": "-1",
                      "ll_client_id": "-1",
                      "line_description": "",
                      "line_type": "",
                      "line_service": "",
                      "line_device": "",
                      "line_room": "",
                      "line_ad_number": "",
                      "ll_id": "",
                      "search_index": "",
                      "search_hostname": "",
                      "red_num": "2",
                      "B1": "change",
                      }
     r2 = session.post(change_url_2, data=change_data_2)
     print(r2.status_code)

# r = session.post(show_url, data=show_network_param)
# print(r.text)

# r = session.post(delete_url, data=delete_data)
# print(r)


class GestIP:
    login_data = ('gipadmin', '123')
    search_data = json.loads('{"search_index": "true", "hostname": "10.0.0.1", "client_id": "1"}')
    login_url = 'http://172.23.8.202/gestioip'
    search_url = 'http://172.23.8.202/gestioip/ip_searchip.cgi'

    def __init__(self):

        self.session = Session()
        self.session.auth = GestIP.login_data
        self.session.post(GestIP.login_url)

    def search(self, ip):
        values = []
        GestIP.search_data['hostname'] = ip
        r = self.session.post(GestIP.search_url, GestIP.search_data)
        html = etree.HTML(r.text)
        all_data = html.xpath('//table[@id="networkTable"]/tr[@valign="middle"]/td')
        name = html.xpath('//table[@id="networkTable"]/tr[@valign="middle"]/td/b')
        for _name in name:
            if _name is not None:
                print(type(_name.text))
                print(_name.text)
                values.append(_name.text)
        # for line in all_data:
        #     txt = line.text
        #     if txt is not None:
        #         if not txt.isspace():
        #             print(txt, end=', ')
        # print()
        print(values)

    def update(self):
        pass

# g = GestIP()
# g.search('10.0.0.1')